const express = require("express")
const router = express.Router()
const multer = require("multer")
const path = require("path")
const {join} = require("path")
const coverDB = require("../../../../db/album")
const fs = require("fs")

// 判断是否有权限
router.use((req,res,next)=>{
  if(!req.session.userInfo.allowDelete){
    return res.send({
      code:15,
      mes:"您没有删除权限，请联系超级管理员进行添加"
    })
  }
  next()
})

// 删除相册
router.post("/delete",async(req,res)=>{
  try {
    let {_id} = req.body
    let doc = await coverDB.findById(_id)
    if(!doc){
      return res.send({
        code:10,
        mes:"该封面相册已被删除，请刷新后重试"
      })
    }
    // 删除数据库
    await coverDB.findByIdAndRemove(_id)
    // 删除服务器文件
    fs.rm(join(__dirname,"../../../../static"+doc.src),{recursive:true},err=>{
      if(err) return
    })
    res.send({
      code:0,
      mes:"该封面相册已删除"
    })
  } catch (error) {
    res.send({
      code:5,
      mes:"服务器异常，请稍后再试"
    })
  }
})



module.exports = router